const axios = require('axios')
const { RunCmd, CmdList } = require('./cmd')
const { Inquirer } = require('./inquirer')
const { key } = require('../config/index')

const pushRepo = async () => {
  // 先让用户选择项目模板
  Inquirer().then(({ name, description, private }) => {
    // 获取Gitee API Token
    // 发送创建仓库请求
    axios({
      method: 'post',
      url: 'https://gitee.com/api/v5/user/repos',
      headers: {
        Authorization: `Bearer ${key}`
      },
      data: {
        name,
        description,
        private
      }
    })
      .then(({ data }) => {
        console.log(`成功创建仓库：${data.html_url}`)
        console.log(`正在上传代码~`)
        for (let cmd of CmdList(data.full_name)) {
          RunCmd(cmd)
        }
        console.log(`上传完成~`)
      })
      .catch((err) => {
        console.error(`创建仓库失败：${err.message}`)
      })
  })
}

module.exports = { pushRepo }
